<?php

function getList($table , $curpage = 1, $perpage = 5)
{
	$offset = ($curpage-1)*$perpage;				//$offset 偏移量，指每页开始拿数据的下标。
	$sql = 'select * from '.$table.' order by id desc limit '.$offset.','.$perpage;
	$query = mysql_query($sql);
	$result = array();
	while ($row = mysql_fetch_assoc($query)) {
		$result[] = $row;
	}
	return $result;
}


function getById($table,$id){
	$sql = 'select * from '.$table.' where id = '.$id;
	$query = mysql_query($sql);
	$result = mysql_fetch_assoc($query);
	return $result;
}

function insert($table,$data){
	$key_str = '';
	$value_str = '';
	foreach ($data as $key => $value) {
		$key_str .= $key.',';
		$value_str .= "'$value',";
	}
	$key_str = substr($key_str,0,-1);
	$value_str = substr($value_str, 0, -1);
	$sql = 'insert into '.$table.' ('.$key_str.')values('.$value_str.')';
	$query = mysql_query($sql);
	$id = mysql_insert_id();
	$result = getById($table,$id);
	return $result;
}